package net.linku.config;

import net.linku.filter.GlobalTraceFilter;
import net.linku.filter.TraceIdFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 过滤器配置类，注册 GlobalTraceFilter 和 TraceIdFilter。
 */
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean<GlobalTraceFilter> globalTraceFilter() {
        FilterRegistrationBean<GlobalTraceFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new GlobalTraceFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(1); // 设置过滤器顺序，确保在所有其它过滤器之前执行
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<TraceIdFilter> traceIdFilter() {
        FilterRegistrationBean<TraceIdFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new TraceIdFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(2); // 设置过滤器顺序，确保在 GlobalTraceFilter 之后执行
        return registrationBean;
    }
}
